#include "stdafx.h"
#include "GrayScaler.h"
#include "imageIterator.h"
#include "cv.h"
#include <omp.h>
#include <iostream>

void GrayScaler::process(IplImage *image)
{
	//opencv method
	//cvCvtColor(image,output,CV_BGR2GRAY);
	int height = image->height;
	int width = image->width;
	int widthStep1 = image->widthStep;
	int widthStep2 = output->widthStep;
	uchar *data1 = (uchar*)image->imageData;
	uchar *data2 = (uchar*)output->imageData;
	int y=0;
	int x=0;

	for( y=0; y<height; y++ )
	{
		uchar* image_ptr = data1 + y * widthStep1;
		uchar* output_ptr = data2+ y * widthStep2;
		for( x=0; x<width; x++ )
		{
			output_ptr[x] = image_ptr[3*x+0]*0.3+image_ptr[3*x+1]*0.59+image_ptr[3*x+2]*0.11; 
		}
	} 
	
}

void GrayScaler::processOpenMP(IplImage *image)
{
	int height = image->height;
	int width = image->width;
	int widthStep1 = image->widthStep;
	int widthStep2 = output->widthStep;
	uchar *data1 = (uchar*)image->imageData;
	uchar *data2 = (uchar*)output->imageData;
	int y=0;
	int x=0;
#pragma omp parallel default(none)\
	shared(data1,data2,height,width,widthStep1,widthStep2) private(y,x)
	{
#pragma omp for nowait
		for( y=0; y<height; y++ )
		{
			uchar* image_ptr = data1 + y * widthStep1;
			uchar* output_ptr = data2+ y * widthStep2;
			for( x=0; x<width; x++ )
			{
				output_ptr[x] = image_ptr[3*x+0]*0.3+image_ptr[3*x+1]*0.59+image_ptr[3*x+2]*0.11; 
			}
		} 
	}
}
